<?php

if (!$_SESSION) session_start();

define('ROOT', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);
define('WWW_ROOT', dirname($_SERVER['PHP_SELF']));
define('MITHOS', ROOT . DS . 'mithos');
define('APP', ROOT . DS . 'app');
define('CONFIG', APP . DS . 'configs' . DS);
define('THEME', APP . DS . 'themes' . DS);
define('HELPER', MITHOS . DS . 'helpers' . DS);
define('COMPONENT', MITHOS . DS . 'components' . DS);

function underscore($string = '') {
	return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $string));
}

function humanize($string = '') {
	return ucwords(str_replace(array('_', '-'), ' ', $string));
}

function pr($data = '') {
    echo '<pre>' . print_r($data, true) . '</pre>';
}

function is_ajax() {
	return array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest';
}

function __autoload($name) {
	$name = underscore($name);
	if (file_exists(MITHOS . DS . $name . '.php')) {
		require_once MITHOS . DS . $name . '.php';
	}
}

Config::load_from_app(array('database', 'settings'));
Config::load_from_theme(array('routes', 'settings'));

if (!Config::read('debug')) {
	ini_set('display_errors', 0);
	error_reporting(false);
}

$dispatcher = new Dispatcher();
$dispatcher->dispatch();

?>